<template>
  <div class="contents">
    <div class="sousuo box-shadow">
      <a-form
          :form="contents"
          :label-col="labelCol"
          layout="inline"
          @submit="handleSearch">
        <a-form-item label="监控点选择" style="margin-right: 8px">
          <a-select v-model:value="contents.select1" @change="monkey">
            <a-select-option :value="null" key="0">全部</a-select-option>
            <a-select-option
                v-for="sec in sese" :key="sec.sectionId" :value="sec.sectionId">
              {{ sec.sectionName }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item style="margin-right: 131px">
          <a-select v-model:value="contents.select2" @change="juns">
            <a-select-option
                v-for="section in sections" :key="section.id" :value="section.id">
              {{ section.name }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="开始时间" style="margin-right: 131px">
          <a-date-picker
              @change="creatChange"
              show-time
              :placeholder="[moment(new Date()).subtract(10, 'days').format('YYYY-MM-DD HH:mm:ss')]"
              format="YYYY-MM-DD HH:mm:ss"
              placeholder="请选择开始时间"/>
        </a-form-item>
        <a-form-item label="结束时间">
          <a-date-picker
              @change="end"
              show-time
              format="YYYY-MM-DD HH:mm:ss"
              :placeholder="[moment(new Date()).subtract(0, 'days').format('YYYY-MM-DD HH:mm:ss')]"
              placeholder="请选择结束时间"/>
        </a-form-item>
        <a-form-item class="form-end">
          <a-button @click="getAll" type="primary">查询</a-button>
        </a-form-item>
      </a-form>
    </div>
    <div class="liebiao box-shadow">
      <div class="fonts">
        <span>查询列表</span>
      </div>
      <a-table
          :scroll="{ y: 520 }"
          :data-source="yyy.weatherInfo"
          @change="tablePaginationChange"
          :pagination="{
          current: yyy.startPage,
          total: yyy.totalCount,
          pageSize: yyy.everyPage,
          showSizeChanger: true,
          showQuickJumper: true,
          pageSizeOptions: ['5', '10', '20', '100'],
          showTotal: (total) => `共${total}条`,
          showSizeChange: (current, pageSize) => {
            this.pageSize = pageSize;
          },
        }">
        <a-table-column title="监控点" data-index="deviceName"></a-table-column>
        <a-table-column title="时间" data-index="insetTime"></a-table-column>
        <a-table-column title="温度(℃)" data-index="temperature"></a-table-column>
        <a-table-column title="湿度(%)" data-index="huminity" width="5%"></a-table-column>
        <a-table-column title="风速风向" data-index="windStr"></a-table-column>
        <a-table-column title="PM2.5(ug/m³)" data-index="pm25"></a-table-column>
        <a-table-column title="PM10(ug/m³)" data-index="pm10"></a-table-column>
        <a-table-column title="噪声(dB)" data-index="noise"></a-table-column>
      </a-table>
    </div>
  </div>
</template>

<script>
import { setHeight } from "@/utils/tableUtils";
import moment from "moment";
import "moment/locale/zh-cn";
import axios from "@/utils/axios";
import { useStore } from "vuex";

import { ref } from "vue";
export default {
  data() {
    return {
      labelCol: { style: { width: "100px" } },
      yyy: [],
      tttt: [],
      sections: [],
      ttt: [],
      tt: [],
      tod: [],
      sese: [],
      yus: [],
      dater: [],
      daters: [],
      contents: {
        select1: "",
        select2: "",
        id: 1,
      },
    };
  },
  methods: {
    moment,
    creatChange(dateString) {
      let a = moment(dateString._d).format("YYYY-MM-DD HH:mm:ss");
      this.dater = a;
    },
    end(dateString) {
      let b = moment(dateString._d).format("YYYY-MM-DD HH:mm:ss");
      this.daters = b;
    },
    juns(pd) {
      let obj = {};
      obj = this.sections.find((item) => {
        return item.id === pd;
      });
      this.yus = obj.id;
      this.tt = this.yus;
    },
    monkey(pdd, value) {
      let objs = {};
      objs = this.sese.find((item) => {
        return item.sectionId === pdd;
      });

      if (value.key == 0) {
        this.tod = null;
        this.contents.select2 = "";
        this.jiance();
      } else {
        this.tod = objs.sectionId;
        this.contents.select2 = "";
        this.jiance();
        this.tt = 0;
      }
    },
    ec() {
      axios
          .post(
              "weather/findHistory",
              JSON.stringify({
                id: this.tt != 0 ? this.tt : 0,
                startTime: this.dater != 0 ? this.dater : moment(new Date()).subtract(10, 'days').format('YYYY-MM-DD HH:mm:ss'),
                endTime: this.daters != 0 ? this.daters : moment(new Date()).subtract(0, 'days').format('YYYY-MM-DD HH:mm:ss'),
                startPage: this.tttt != 0 ? this.tttt : 1,
                everyPage: this.ttt != 0 ? this.ttt : 50,
              }),
              {
                headers: {
                  "Content-Type": "application/json",
                },
              }
          )
          .then((res) => {
            this.yyy = res.data;
          });
    },
    jiance() {
      axios
          .post(
              "/weather/findAllStation",
              JSON.stringify({
                sectionId: this.tod != 0 ? this.tod : null,
              }),
              {
                headers: {
                  "Content-Type": "application/json",
                },
              }
          )
          .then((ress) => {
            this.sections = ress.data;
            this.tt = this.sections[0].id;
            this.contents.select2 = this.sections[0].name;
            this.ec();
          });
    },
    tablePaginationChange(u) {
      this.tttt = u.current;
      this.ttt = u.pageSize;
      this.ec();
    },
    de() {
      const store = useStore();
      const sese = ref(
          JSON.parse(JSON.stringify(store.getters["userinfo/getUserSection"]))
      );
      this.sese = sese;
    },
    getAll() {
      this.ec();
    },
  },
  mounted() {
    this.ec();
    this.de();
    this.jiance();
    this.contents.select1 = null;
  },
};
</script>

<style scoped lang="scss">
.contents {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  .sousuo {
    display: flex;
    width: 100%;
    height: 72px;
    align-items: center;
    padding: 0 20px;
    box-sizing: border-box;
    flex-shrink: 0;
    ::v-deep label{
      color: #666;
    }
    ::v-deep .ant-form{
      width: 100%;
    }
    ::v-deep .ant-select{
      width: 196px;
    }
    .form-end{
      float: right;
      margin-right: 13px;
      ::v-deep button{
        margin-left: 12px;
      }
    }
  }
  .liebiao {
    display: flex;
    flex-direction: column;
    width: 100%;
    flex: 1;
    margin-top: 20px;
    padding: 0 38px;
    box-sizing: border-box;
    .fonts {
      display: flex;
      width: 100%;
      padding: 18px 0 12px;
      flex-direction: row;
      justify-content: flex-start;
      box-sizing: border-box;
      span{
        display: block;
        font-size: 18px;
        font-weight: bold;
        line-height: 32px;
      }
    }
  }
}
</style>>